//
//  PPFriendsTimelineTableViewSectionHeaderView.m
//  pickpic
//
//  Created by Jinxiao on 10/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumActivityTimelineTableViewSectionHeaderView.h"

#import "PPProfileViewController.h"
#import "PPTimelineLikesViewController.h"
#import "PPTimelineFavouritesViewController.h"
#import "PPCommentTimelineViewController.h"
#import "PPTimelinePictureViewController.h"
#import "PPTimelineDetailViewController.h"

#import "PPActivityCommitManager.h"

#import "PPPictureLoadingProgressView.h"

#define insets UIEdgeInsetsMake(15, 15, 0, 15)
#define verticalSpacing 15

@interface PPAlbumActivityTimelineTableViewSectionHeaderView ()
@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (weak, nonatomic) IBOutlet UILabel *nickLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *coverViewHeight;
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
@property (weak, nonatomic) IBOutlet UIView *coverContainerView;

@property (strong, nonatomic) IBOutlet UIButton *textHeightConstraint;

@property (weak, nonatomic) IBOutlet DGAlignmentButton *likeCountButton;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;

@property (readwrite, nonatomic, strong) DGCoreTextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeButtonTop;

@property (readwrite, nonatomic, strong) PPPictureLoadingProgressView *progressView;
@property (readwrite, nonatomic, strong) UIImageView *statusView;

@end

@implementation PPAlbumActivityTimelineTableViewSectionHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];

    [self addTopBorderWithColor:kLineColor andWidth:kLineWidth];

    _textView = [[DGCoreTextView alloc] init];
    _textView.backgroundColor = [UIColor clearColor];
    [self addSubview:_textView];

    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = 18.f;

    __weak typeof(self)weakSelf = self;
    [_avatarView setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];

    [_nickLabel setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];

    _progressView = [[PPPictureLoadingProgressView alloc] init];
    _progressView.trackColor = [UIColor colorWithRGBHex:0xd9d9d9];
    _progressView.valueColor = [UIColor colorWithRGBHex:0xc5c5c5];
    [_coverImageView addSubview:_progressView];

    _statusView = [[UIImageView alloc] init];
    _statusView.backgroundColor = [UIColor clearColor];
    _statusView.image = [UIImage imageNamed:@"qian"];
    [self addSubview:_statusView];
    
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(60));
        make.height.equalTo(@(60));
        make.center.equalTo(_coverImageView);
    }];

    [_statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_statusView.superview);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    if(_activity.editor.styledString.length > 0) {
        CGSize briefSize = [_activity.editor sizeThatFits:CGSizeMake(self.width - insets.left - insets.right, 1000)];
        _textView.frame = CGRectMake(insets.left, _coverContainerView.bottom + verticalSpacing, briefSize.width, briefSize.height);

        _likeButtonTop.constant = _textView.height + 11;
    } else {
        _likeButtonTop.constant = 0;
    }
}

- (void)portraitDidPress {
    [MobClick event:@"93"];
    PPProfileViewController *vc = [[PPProfileViewController alloc] init];
    vc.userID = _activity.user.userID;
    [_delegateViewController.navigationController pushViewController:vc animated:YES];
}

- (void)setActivity:(PPTimelineAlbumActivityObject *)activity {
    _activity = activity;

    _progressView.hidden = YES;
    _statusView.hidden = YES;
    
    PPAlbumObject *album = activity.album;

    // avatar
    {
        if(album.ownerAvatar.length > 0) {
            [album.ownerAvatar applyToImageView:_avatarView];
        } else {
            _avatarView.image = [UIImage imageNamed:@"icon-person-70"];
        }
    }


    // nick
    {
        _nickLabel.text = album.ownerName;
    }

    // date
    {
        _dateLabel.text = [activity.date timeAgoForTimeline];
    }

    // audit
    {
        if(album.audit == 2) {
            _coverImageView.image = [UIImage imageNamed:@"ban-2"];
            _coverImageView.contentMode = UIViewContentModeCenter;
            [_coverImageView setActionBlock:nil forEvent:UIControlEventTouchUpInside];
        } else {
            _coverImageView.contentMode = UIViewContentModeScaleAspectFill;

            __weak typeof(self)weakSelf = self;

            switch(activity.activityType) {
                case PPTimelineAlbumActivityTypeAdded: {

                    NSString *URLString = activity.pictures.firstObject.URLString;

                    if(URLString.length > 0) {
                        weakSelf.progressView.hidden = NO;

                        [URLString applyToImageView:_coverImageView placeholder:nil progressive:^(double progress) {
                            weakSelf.progressView.progress = progress;
                        } errorCompletion:^(NSError *error) {
                            weakSelf.progressView.hidden = YES;

                            if(error.code == 404) {
                                _statusView.hidden = NO;
                            }
                        }];
                    }

                    [_coverImageView setActionBlock:^(UIControl *control) {
                        if(activity.pictures.count == 0) {
                            [weakSelf.delegateViewController.navigationController.view toastMessage:@"图册暂时还没有图片哦"];
                        } else {
                            PPTimelinePictureViewController *pictureViewController = [[PPTimelinePictureViewController alloc] init];
                            pictureViewController.pictures = activity.pictures;
                            pictureViewController.ownerID = activity.user.userID;
                            pictureViewController.albumTitle = activity.album.title;
                            [weakSelf.delegateViewController.navigationController pushViewController:pictureViewController animated:YES];
                        }
                    } forEvent:UIControlEventTouchUpInside];

                    break;
                }

                default: {

                    NSString *URLString = album.existingCoverURLString;

                    if(URLString.length > 0) {
                        weakSelf.progressView.hidden = NO;

                        [URLString applyToImageView:_coverImageView placeholder:nil progressive:^(double progress) {
                            weakSelf.progressView.progress = progress;
                        } completion:^{
                            weakSelf.progressView.hidden = YES;
                        }];
                    }

                    [_coverImageView setActionBlock:^(UIControl *control) {
                        [MobClick event:@"102"];

                        if(activity.album.pictures.count == 0) {
                            [weakSelf.delegateViewController.navigationController.view toastMessage:@"图册暂时还没有图片哦"];
                        } else {
                            PPTimelineDetailViewController *detailViewController = [[PPTimelineDetailViewController alloc] init];
                            detailViewController.album = activity.album;
                            [weakSelf.delegateViewController.navigationController pushViewController:detailViewController animated:YES];
                        }
                    } forEvent:UIControlEventTouchUpInside];

                    break;
                }

            }
        }
    }

    _coverViewHeight.constant = [UIScreen mainScreen].bounds.size.width;

    // description
    {
        _textView.editor = activity.editor;
    }

    _likeButton.selected = album.isLiked;

    [_likeCountButton setTitle:[NSString stringWithFormat:@"%ld", album.like] forState:UIControlStateNormal];
    _likeCountButton.hidden = album.like > 0 ? NO : YES;
    [self setNeedsLayout];
}

- (IBAction)moreButtonDidPress:(id)sender {

}

- (IBAction)likeButtonDidPress:(id)sender {

    if(_likeButton.selected == YES) {
        [MobClick event:@"96"];
        _likeButton.selected = NO;

        _activity.album.like--;
        _activity.album.isLiked = NO;
        [[PPActivityCommitManager sharedInstance] offlineUnlikeAlbumWithAlbumID:_activity.album.albumID.integerValue version:_activity.version];
    } else {
        [MobClick event:@"95"];
        _likeButton.selected = YES;
        _activity.album.like++;
        _activity.album.isLiked = YES;
        [[PPActivityCommitManager sharedInstance] offlineLikeAlbumWithAlbumID:_activity.album.albumID.integerValue version:_activity.version];
    }

    [_likeCountButton setTitle:[NSString stringWithFormat:@"%ld", _activity.album.like] forState:UIControlStateNormal];
    _likeCountButton.hidden = _activity.album.like > 0 ? NO : YES;
}

- (IBAction)commentButtonDidPress:(id)sender {
    [MobClick event:@"97"];
    [_delegate sectionHeaderView:self didSelectCommentListForActivity:_activity];
}

- (IBAction)likeCountButtonDidPress:(id)sender {
    PPTimelineLikesViewController *vc = [[PPTimelineLikesViewController alloc] init];
    vc.version = _activity.version;
    vc.type = @1;
    vc.ID = _activity.album.albumID;
    [_delegateViewController.navigationController pushViewController:vc animated:YES];
}

+ (CGFloat)heightForActivity:(PPTimelineAlbumActivityObject *)activity {
    CGFloat height = 0;

    height += 46;
    height += [UIScreen mainScreen].bounds.size.width;
    height += verticalSpacing;

    CGSize briefSize = [activity.editor sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width - insets.left - insets.right, 1000)];
    height += briefSize.height;

    height += 42;

    return (NSInteger)round(height);
}

@end
